package com.hunttown.mes.manage.controller._lbs;

import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.LbsTerminalRegisterLogManageService;
import com.hunttown.mes.rpc.domain.LbsTerminalRegisterLogDTO;
import com.hunttown.mes.rpc.domain.enums.lbs.RegisterLogTypeEnum;
import com.hunttown.mes.rpc.domain.enums.lbs.ResponseAuthEnum;
import com.hunttown.mes.rpc.domain.enums.lbs.ResponseResultEnum;
import com.hunttown.mes.rpc.domain.query.LbsTerminalRegisterLogDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * created by wangjunfu on 2022-01-17 10:16:09
 */
@Controller
@RequestMapping(value = "/lbs/register")
public class TerminalRegisterLogController {

    private final static Logger logger = LoggerFactory.getLogger(TerminalRegisterLogController.class);

    private final LbsTerminalRegisterLogManageService lbsTerminalRegisterLogService;
    private final ModelService modelService;

    @Autowired
    public TerminalRegisterLogController(LbsTerminalRegisterLogManageService lbsTerminalRegisterLogService, ModelService modelService) {
        this.lbsTerminalRegisterLogService = lbsTerminalRegisterLogService;
        this.modelService = modelService;
    }

    //region 数据列表

    /**
     * 首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @return model
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, LbsTerminalRegisterLogDTOQuery dtoQuery) {

        dtoQuery.setOrderBy("order by id desc");
        Page<LbsTerminalRegisterLogDTO> dataPage = lbsTerminalRegisterLogService.getForPage(dtoQuery);

        for (LbsTerminalRegisterLogDTO item : dataPage.getItems()) {
            item.setLogSignName(RegisterLogTypeEnum.getEnumByKey(item.getLogSign()));

            if (item.getLogSign() == 1) { //终端注册
                item.setResultName(ResponseResultEnum.getEnumByKey(item.getResult()));
            } else { //其它
                item.setResultName(ResponseAuthEnum.getEnumByKey(item.getResult()));
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        return "lbs/register/index";
    }
    //endregion

    //region 数据查看

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return model
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        LbsTerminalRegisterLogDTO obj = new LbsTerminalRegisterLogDTO();
        obj.setId(id);

        if (id > 0) {
            obj = lbsTerminalRegisterLogService.getById(id);
        }
        lbsTerminalRegisterLogService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }

    //endregion

}